<?php
declare(strict_types=1);
namespace app\api\service;

use app\common\model\WaterModel;

class WaterService extends CommonService
{

    public function water(array $params)
    {
        $water = new WaterModel();
        $data = $water->getWaterData($params);
        if (!$data) {
            $water->insertWaterData($params['robot_wxid'], $params['final_from_wxid']);
            $message = "========================\r" .
                "已为您加入每日八杯水提醒服务~\r" .
                "关闭请输入`关闭喝水提醒` 将不再提醒\r" .
                "========================";
        }else{
            if ($data['type'] == 0) {
                $water->setWaterData($params);
                $message = "========================\r" .
                    "已为您重新开通每日八杯水提醒服务~\r" .
                    "关闭请输入`关闭喝水提醒` 将不再提醒\r" .
                    "========================";
            } else {
                $message = "========================\r" .
                    "您已经开通了提醒服务~\r" .
                    "关闭请输入`关闭喝水提醒` 将不再提醒\r" .
                    "========================";
            }
        }
        return $this->send_text_msg($params['robot_wxid'], $params['final_from_wxid'], $message);
    }

    public function cancelWater(array $params)
    {
        $water = new WaterModel();
        $data = $water->getWaterData($params);
        trace($data);
        if (!$data || $data['type'] == 0) {
            $message = "========================\r" .
                "您还未加入每日八杯水提醒服务~\r" .
                "输入`喝水提醒` 将为你开启提醒服务\r" .
                "========================";
        }elseif($data['type'] == 1){
            $water->setWaterData($params, ['type' => 0]);
            $message = "========================\r" .
                "已为您关闭每日八杯水提醒服务~\r" .
                "输入`喝水提醒` 将为你开启提醒服务\r" .
                "========================";
        }
        if(!empty($message)){
            return  $this->send_text_msg($params['robot_wxid'], $params['final_from_wxid'], $message);
        }
    }
}